<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Donvitructhuoc Model
 */
class VastModelDonvitructhuoc extends JModelItem {

    /**
     * @var string donvis
     */
    private $_total = null;
    private $_pagination = null;
    protected $donvis;
    protected $khoi;

    public function __construct() {
        parent::__construct();

        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));

        $id = JRequest::getInt('id');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->where('a.khoi_id = ' . $id);

        $db->setQuery($query);
        $this->donvis = $db->loadObjectList();
        
        $this->_total = count($this->donvis);

    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getDonvis() {
        // get danh sach don vi truc thuoc khoi
        $id = JRequest::getInt('id');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');
        
        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->where('a.khoi_id = ' . $id . ' LIMIT ' . $limitstart . ',' . $limit);

        $db->setQuery($query);
        $this->donvis = $db->loadObjectList();

        return $this->donvis;
    }

    public function getKhoi() {

        $id = JRequest::getInt('id');
        // get thong tin khoi
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__khoi AS a');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $this->khoi = $db->loadObject();

        return $this->khoi;
    }

    public function getPagination() {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal() {
        return $this->_total;
    }

}